﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Speech.Synthesis;
using System.Speech;
using System.Speech.Recognition;

namespace CWCS.BLL
{
    public class QueueSpeech
    {
        static SpeechSynthesizer voice = new SpeechSynthesizer();
        private static Queue<string> ListQueue = new Queue<string>();
        public static void AddQueue(string msg) //入列  
        {
            if (!ListQueue.Contains(msg) && ListQueue.Count < 3)
                ListQueue.Enqueue(msg);

            //testlog.Info(string.Format("准备进队列内容:{0}，当前队列缓存数:{1}",msg, ListQueue.Count.ToString()));
        }

        public static void Start()//启动  
        {
            Thread thread = new Thread(threadStart);
            thread.IsBackground = true;
            thread.Start();
        }

        private static void threadStart()
        {
            while (true)
            {
                if (ListQueue.Count > 0)
                {
                    try
                    {
                        ScanQueue();
                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    //没有任务，休息5秒钟  
                    Thread.Sleep(5000);
                }
            }
        }

        private static void ScanQueue()//出列
        {
            while (ListQueue.Count > 0)
            {
                try
                {
                    string msg = ListQueue.Dequeue();
                    voice.SpeakAsync(msg);
                    //Thread.Sleep(100);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
    }
}
